package com.aiyige.page.my.message.notifymessage.ordermessage;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.api.ApiManager;
import com.aiyige.model.Page;
import com.aiyige.model.message.entity.Message;
import com.aiyige.model.message.view.OrderContent;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.page.my.message.notifymessage.ordermessage.adapter.OrderMessageAdapter;
import com.aiyige.page.my.message.notifymessage.ordermessage.model.OrderMessage;
import com.aiyige.page.my.message.util.MessageUtil;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.widget.CommonDataView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.sql.Struct;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;

@Route(path = ARouterConfig.OrderMessagePage)
public class OrderMessagePage extends AppCompatActivity implements CommonDataView.RetrofitStub {

    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.cdv)
    CommonDataView cdv;
    OrderMessageAdapter orderMessageAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_order_message);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        titleTv.setText(R.string.order);
        orderMessageAdapter = new OrderMessageAdapter();
        orderMessageAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                try {
                    OrderMessage orderMessage = orderMessageAdapter.getItem(position);
                    switch (orderMessage.getOrderStatus()) {
                        case MessageUtil.ORDER_STATUS_REFUNDING:
                            Router.start(orderMessage.getRefundRouter());
                            break;
                        default:
                            Router.start(orderMessage.getRouter());
                            break;
                    }
                } catch (Exception e) {
                }
            }
        });

        orderMessageAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                try {
                    switch (view.getId()) {
                        case R.id.otherHeadIv:
                        case R.id.otherNameTv: {
                            OrderMessage orderMessage = orderMessageAdapter.getItem(position);
                            ARouter.getInstance()
                                    .build(ARouterConfig.UserPage)
                                    .withString("userId", orderMessage.getOtherId())
                                    .navigation();
                            break;
                        }
                        case R.id.orderLayout: {
                            OrderMessage orderMessage = orderMessageAdapter.getItem(position);
                            Router.start(orderMessage.getRouter());
                            break;
                        }
                    }
                } catch (Exception e) {

                }
            }
        });
        cdv.config(orderMessageAdapter, this);
        cdv.doRefreshRequest();
    }

    @OnClick({R.id.titleBackBtn})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
        }
    }

    @Override
    public Call<ResponseBody> call(long currentPage) {
        return ApiManager.getService().message(Message.TYPE_ORDER, 15, currentPage);
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            Page page = JSON.parseObject(respJson, Page.class);
            List<Message> messageList = new LinkedList<>();
            if (!TextUtils.isEmpty(page.getContent())) {
                messageList.addAll(JSON.parseArray(page.getContent(), Message.class));
            }

            for (Message message : messageList) {
                OrderContent record = null;
                try {
                    record = message.getContent().toJavaObject(OrderContent.class);
                } catch (Exception e) {

                }
                if (record == null) {
                    continue;
                }
                int targetType;
                switch (record.getMoment().getSubject()) {
                    case Moment.SUBJECT_ARTICLE:
                        targetType = MessageUtil.TARGET_TYPE_INFO;
                        break;
                    case Moment.SUBJECT_VIDEO:
                        targetType = MessageUtil.TARGET_TYPE_VIDEO;
                        break;
                    case Moment.SUBJECT_PHOTO:
                        targetType = MessageUtil.TARGET_TYPE_PHOTO;
                        break;
                    case Moment.SUBJECT_VIDEO_COURSE:
                        targetType = MessageUtil.TARGET_TYPE_LEARN_VIDEO;
                        break;
                    case Moment.SUBJECT_MAJOR_COURSE:
                        targetType = MessageUtil.TARGET_TYPE_MAJOR_COURSE;
                        break;
                    case Moment.SUBJECT_SPECIAL_TOPIC:
                        targetType = MessageUtil.TARGET_TYPE_SPECIAL_TOPIC;
                        break;
                    case Moment.SUBJECT_TRAINING_COURSE:
                        targetType = MessageUtil.TARGET_TYPE_TRAING_CARD;
                        break;
                    case Moment.SUBJECT_PRIVATE_COURSE:
                        targetType = MessageUtil.TARGET_TYPE_PERSONAL_TRAINER;
                        break;
                    default:
                        targetType = MessageUtil.TARGET_TYPE_COMMENT;
                        break;
                }
                int orderStatus = 0;
                String otherOrderMessage = "";
                switch (record.getStatus()) {
                    case OrderContent.ORDER_STATUS_UNPAY:
                        otherOrderMessage = StringUtils.getString(R.string.order_message_status_nopay);
                        orderStatus = OrderContent.ORDER_STATUS_UNPAY;
                        break;
                    case OrderContent.ORDER_STATUS_PAYED:
                        otherOrderMessage = StringUtils.getString(R.string.order_message_status_payed);
                        orderStatus = OrderContent.ORDER_STATUS_PAYED;
                        break;
                    case OrderContent.ORDER_STATUS_CONFIRM:
                        otherOrderMessage = StringUtils.getString(R.string.order_message_status_confirmed, StringUtils.priceFormat(record.getAmount()));
                        orderStatus = OrderContent.ORDER_STATUS_CONFIRM;
                        break;
                    case OrderContent.ORDER_STATUS_REFUNDED:
                        otherOrderMessage = StringUtils.getString(R.string.order_message_status_refunded);
                        orderStatus = OrderContent.ORDER_STATUS_REFUNDED;
                        break;
                    case OrderContent.ORDER_STATUS_REFUNDING:
                        otherOrderMessage = StringUtils.getString(R.string.order_message_status_refunding);
                        orderStatus = OrderContent.ORDER_STATUS_REFUNDING;
                        break;
                    case OrderContent.ORDER_STATUS_REJECT_REFUND:
                        otherOrderMessage = StringUtils.getString(R.string.order_message_status_reject_refund);
                        orderStatus = OrderContent.ORDER_STATUS_REJECT_REFUND;
                        break;
                }

                resultList.add(OrderMessage.newBuilder()
                        .otherAvatar(record.getSender().getAvatar())
                        .otherId(record.getSender().getId())
                        .otherName(record.getSender().getName())
                        .goodsId(record.getMoment().getId())
                        .otherTime(message.getCreateTime())
                        .mediaReleaseDate(record.getMoment().getCreateTime())
                        .cover(record.getMoment().getCover())
                        .targetType(targetType)
                        .mediaTitle(record.getMoment().getTitle())
                        .orderNo(record.getCode())
                        .orderId(record.getId())
                        .refundRouter(record.getRefundRouter())
                        .router(record.getRouter())
                        .otherOrderMessage(otherOrderMessage)
                        .orderStatus(orderStatus)
                        .rejectReason(record.getRefundRejectReason())
                        .build());
            }
            return new CommonDataView.HandleResult(page.isLast());
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }
    }
}
